home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_319 / cnewssrc / cnews.orig.lzh / conf / subst < prev    next >
Text File  |  1989-06-27  |  1KB  |  84 lines

  1. #! /bin/sh
  2. # subst - substitute strings into files, carefully
  3.  
  4. PATH=/bin:/usr/bin ; export PATH
  5.  
  6. case "$1"
  7. in
  8.     -f)
  9.     substs=$2
  10.     shift ; shift
  11.     ;;
  12.  
  13.     *)
  14.     echo "$0: no substitutions file given" >&2
  15.     exit 2
  16.     ;;
  17. esac
  18.  
  19. them="`sed '/^#/d; s/^\\([^    ]*\\)        *\\([^    ]*\\)$/s#@<\\1>@#\\2#g/' $substs`"
  20.  
  21. for f
  22. do
  23.     # first, figure out temporary names
  24.     case "$f"
  25.     in
  26.         */*)
  27.         file="`expr \"$f\" : '.*/\\([^/]*\\)'`"
  28.         dir="`expr \"$f\" : '\\(.*\\)/[^/]*'`"
  29.         new="$dir/n.$file"
  30.         old="$dir/o.$file"
  31.         ;;
  32.  
  33.         *)
  34.         new="n.$f"
  35.         old="o.$f"
  36.         ;;
  37.     esac
  38.     echo "$f:"
  39.  
  40.     # test existences
  41.     if test ! -f $f
  42.     then
  43.         echo "$0: cannot find \`$f'" >&2
  44.         continue                # NOTE CONTINUE
  45.     fi
  46.     if test -r $new
  47.     then
  48.         echo "$0: $new exists, cannot proceed" >&2
  49.         exit 1
  50.     fi
  51.     if test -r $old
  52.     then
  53.         echo "$0: $old exists, cannot proceed" >&2
  54.         exit 1
  55.     fi
  56.     ( >$old >$new ) 2>/dev/null
  57.     if test ! -w "$old" -o ! -w "$new"
  58.     then
  59.         rm -f $old $new
  60.         echo "$0: cannot create temporaries $old $new" >&2
  61.         exit 1
  62.     fi
  63.  
  64.     # generate the new version
  65.     trap "rm -f $new; exit" 1 2 15
  66.     sed "/=()<.*>()=/{
  67.         h
  68.         n
  69.         g
  70.         s/.*=()<//
  71.         s/>()=.*//
  72.         $them
  73.     }" $f >$new
  74.  
  75.     # substitute new for old
  76.     trap "mv $old $f; exit" 1 2 15
  77.     mv $f $old
  78.     mv $new $f
  79.  
  80.     # dispense with the old version
  81.     trap "rm -f $old; exit" 1 2 15
  82.     rm $old
  83. done
  84.